﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using shfa.snomtastic.data.business;

namespace shfa.snomtastic.phone
{
    public partial class firmware : System.Web.UI.Page
    {

        public string host { get; set; }
        public string macAddress { get; set; }

        public AppConfig ac = new AppConfig();


        protected void Page_Load(object sender, EventArgs e)
        {
            macAddress = Request.QueryString["mac"];
            host = Request.Url.Host;
        }


        protected void Page_PreRender(object sender, EventArgs e)
        {

            Response.ContentType = "text/xml";
            Response.ContentEncoding = Encoding.UTF8;


            using (TextWriter textWriter = new StreamWriter(Response.OutputStream, Encoding.UTF8))
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.OmitXmlDeclaration = false;
                writerSettings.CloseOutput = true;

                XmlWriter xmlWriter = XmlTextWriter.Create(textWriter, writerSettings);


                //Write the root element

                xmlWriter.WriteStartElement("firmware-settings");
                xmlWriter.WriteStartElement("firmware");
                xmlWriter.WriteAttributeString("perm","");
                xmlWriter.WriteValue(GetFirmwareUpdateUrl());
                xmlWriter.WriteEndElement();

                xmlWriter.WriteEndElement();
                xmlWriter.Close();
            }


        }

        protected void WriteSetting(ref XmlWriter w, string sName, string sSecurity, string sIndex, string sValue)
        {
            w.WriteStartElement(sName);
            w.WriteAttributeString("perm", sSecurity);
            if (sIndex != string.Empty)
            {
                w.WriteAttributeString("idx", sIndex);
            }
            w.WriteValue(sValue);
            w.WriteEndElement();

        }


        protected string GetFirmwareUpdateUrl()
        {
            string UpdateTo = "";
            PhoneController pc = new PhoneController();
            PhoneInfo pi = pc.SelectPhoneByMacAddress(macAddress);

            if (pi != null)
            {
                if (pi.FirmwareId != Guid.Empty)
                {
                    FirmwareController fc = new FirmwareController();
                    FirmwareInfo fi = fc.SelectFirmware(pi.FirmwareId);
                    if (fi!=null)
                    {
                        if (fi.UpgardeTo!=Guid.Empty)
                        {
                            FirmwareInfo fiUpdate = fc.SelectFirmware(fi.UpgardeTo);
                            if (fiUpdate != null)
                            {
                                UpdateTo = ac.Items["siteRoot"].Replace("https:", "http:") + "firmware/" + fiUpdate.FileUrl;
                            }
                        }
                    }
                }
            }
            return UpdateTo;
        }
    }
}
